package swing;

import app.Main;
import packages.db.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.regex.Pattern;


import static app.Main.viewChange;

public class RegisterView extends JPanel{
    public JPanel JPanel0;
    private JPanel JPanel1;
    private JPanel JPanel2;
    private JPanel JPanel3;

    private JButton ReturnButton;
    private JButton RegisterButton;

    private JLabel NameLabel;
    private JLabel PasswordLabel1;
    private JLabel PasswordLabel2;
    private JLabel PhoneLabel;
    private JLabel TitleLabel;

    private JTextField NameField;
    private JPasswordField PasswordField1;
    private JPasswordField PasswordField2;
    private JTextField PhoneField;

    String username;
    String phoneNumber;
    String password;
    String passwordAgain;


    public RegisterView() throws IOException {
        Font font=new Font("楷体", Font.BOLD,35);
        TitleLabel.setFont(font);
        font=new Font("楷体", Font.BOLD,20);
        NameLabel.setFont(font);
        PasswordLabel1.setFont(font);
        PasswordLabel2.setFont(font);
        PhoneLabel.setFont(font);
        RegisterButton.setFont(font);
        ReturnButton.setFont(font);
//        Color color = new Color(0xff, 0xf5, 0xee);
        Color color = new Color(0xff, 0xff, 0xff);
        ReturnButton.setBackground(color);
        RegisterButton.setBackground(color);

        JPanel0.setOpaque(false);
        JPanel1.setOpaque(false);
        JPanel2.setOpaque(false);
        JPanel3.setOpaque(false);

        RegisterButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 得到所有字段
                username = NameField.getText();
                password = new String(PasswordField1.getPassword());
                passwordAgain = new String(PasswordField2.getPassword());
                phoneNumber = PhoneField.getText();

                // 检查格式
                if(!userNameMatch(username)){
                    // 用户名格式错误
                    JOptionPane.showMessageDialog(null,"注册失败!\n用户名应为5-15位,以字母开头且只包含字母 数字 _ -!","提示",
                            JOptionPane.ERROR_MESSAGE);
                }
                else if(!passwordMatch(password)){
                    // 密码格式错误
                    JOptionPane.showMessageDialog(null,"注册失败!\n密码应为5-15位,只包含字母 数字 _-,./@#","提示",
                            JOptionPane.ERROR_MESSAGE);
                }
                else if(!password.equals(passwordAgain)){
                    //两次输入密码不相等
                    JOptionPane.showMessageDialog(null,"注册失败!\n两次密码输入不一致!","提示",
                            JOptionPane.ERROR_MESSAGE);
                }else {
                    // 格式正确

                    if(phoneNumber==""){
                        phoneNumber = null;
                    }
                    //插入本地数据库
                    DBUtil db = new DBUtil();
                    //更新操作（增加数据）
                    Object[] obj = {null, username, password, phoneNumber};
                    int res = db.update("insert into user values(?,?,?,?)", obj);
                    db.closeConnection();
                    if(res==1){
                        JOptionPane.showMessageDialog(null,"注册成功!\n欢迎你成为校园的护猫使者~","提示",
                                JOptionPane.PLAIN_MESSAGE);
                        NameField.setText("");
                        PasswordField1.setText("");
                        PasswordField2.setText("");
                        PhoneField.setText("");
                    }
                    else{
                        JOptionPane.showMessageDialog(null,"注册失败!","提示",
                                JOptionPane.ERROR_MESSAGE);
                    }

                }
            }
        });
        ReturnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                viewChange = 0;
                synchronized (Main.object){
                    Main.object.notify();
                }
            }
        });
    }

    // 用于匹配用户名是否符合规范 5-15位 以字母开头且只包含字母 数字 _
    private boolean userNameMatch(String userName){
        String pattern = "^[a-zA-Z][a-zA-Z0-9_-]{4,14}$";
        return (Pattern.matches(pattern, userName));
    }

    // 用于匹配密码是否符合规范 5-15位 只包含字母 数字 - _ , . / @ #
    private boolean passwordMatch(String password){
        String pattern = "^[a-zA-Z0-9_,./@#-]{5,15}$";
        return (Pattern.matches(pattern, password));
    }
}
